/**
 * 前端测试环境设置文件
 */
import { config } from '@vue/test-utils'
import { vi } from 'vitest'

// 全局测试配置
config.global.mocks = {
  $t: (msg: string) => msg
}

// 全局模拟对象
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: vi.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: vi.fn(), // deprecated
    removeListener: vi.fn(), // deprecated
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
    dispatchEvent: vi.fn(),
  })),
})

// 模拟浏览器环境下的全局对象
if (typeof window !== 'undefined') {
  // 模拟 ResizeObserver
  if (!window.ResizeObserver) {
    ;(window as any).ResizeObserver = vi.fn().mockImplementation(() => ({
      observe: vi.fn(),
      unobserve: vi.fn(),
      disconnect: vi.fn(),
    }))
  }

  // 模拟 IntersectionObserver
  if (!window.IntersectionObserver) {
    ;(window as any).IntersectionObserver = vi.fn().mockImplementation(() => ({
      observe: vi.fn(),
      unobserve: vi.fn(),
      disconnect: vi.fn(),
    }))
  }
}

// 模拟 console 方法以避免测试中的干扰
console.warn = vi.fn()
console.error = vi.fn()